﻿Module modJournalEntry


    Public Function JournalEntry_Posting(ByVal oDVJournal As DataView, ByRef oCompany As SAPbobsCOM.Company, ByRef sErrDesc As String) As Long

        Dim sFuncName As String = String.Empty
        Dim sEntity As String = String.Empty
        Dim ddate As Date = Nothing
        Dim ival As Integer
        Dim IsError As Boolean
        Dim iErr As Integer = 0
        Dim sErr As String = String.Empty
        Dim sJV As String = String.Empty

        Try
            sFuncName = "JournalEntry_Posting"
            Console.WriteLine("Starting Function ", sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Starting Function ", sFuncName)
            'Fetching Entity from the Dataview object

            ddate = oDVJournal.Table.Rows(1).Item(2).ToString.Substring(0, 2) & "/" & oDVJournal.Table.Rows(1).Item(2).ToString.Substring(2, 2) & "/" & oDVJournal.Table.Rows(1).Item(2).ToString.Substring(4, 4)
            Dim oJournalEntry As SAPbobsCOM.JournalEntries = oCompany.GetBusinessObject(SAPbobsCOM.BoObjectTypes.oJournalEntries)
            ' Journal Entry Document Header Information
            oJournalEntry.ReferenceDate = ddate
            oJournalEntry.DueDate = ddate
            oJournalEntry.TaxDate = ddate
            oJournalEntry.Reference = oDVJournal.Table.Rows(1).Item(1).ToString.Trim
            oJournalEntry.Memo = oDVJournal.Table.Rows(1).Item(5).ToString.Trim

            'Journal Entry Document Line Information

            For Each drv As DataRowView In oDVJournal
                'Account Code
                oJournalEntry.Lines.AccountCode = drv(8).ToString.Trim
                ' Debit Amount
                If Not String.IsNullOrEmpty(drv(9).ToString.Trim) Then
                    oJournalEntry.Lines.Debit = CDbl(drv(9).ToString.Trim)
                    oJournalEntry.Lines.Credit = "0"
                End If
                'Credit Amount
                If Not String.IsNullOrEmpty(drv(10).ToString.Trim) Then
                    oJournalEntry.Lines.Debit = "0"
                    oJournalEntry.Lines.Credit = CDbl(drv(10).ToString.Trim)
                End If
                'Line Remarks
                oJournalEntry.Lines.LineMemo = drv(11).ToString.Trim
                'Line Project Code ( Dimension 1)
                If Not String.IsNullOrEmpty(drv(13).ToString.Trim) Then
                    oJournalEntry.Lines.CostingCode = drv(13).ToString.Trim
                End If
                'Line Profit Center ( Dimension 2)
                If Not String.IsNullOrEmpty(drv(14).ToString.Trim) Then
                    oJournalEntry.Lines.CostingCode2 = drv(14).ToString.Trim
                End If

                oJournalEntry.Lines.Add()
            Next

            Console.WriteLine("Attempting to Add the Journal Entry ", sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Attempting to Add the Journal Entry", sFuncName)
            ival = oJournalEntry.Add()

            If ival <> 0 Then
                IsError = True
                oCompany.GetLastError(iErr, sErr)
                p_sJournalEntryError = "Error Code :- " & iErr & " Error Description :- " & sErr
                Call WriteToLogFile("Completed with ERROR ---" & sErr, sFuncName)
                Console.WriteLine("Completed with ERROR ", sFuncName)
                If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR " & sErr, sFuncName)
                JournalEntry_Posting = RTN_ERROR
                Exit Function
            End If

            Console.WriteLine("Completed with SUCCESS", sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with SUCCESS ", sFuncName)
            oCompany.GetNewObjectCode(sJV)
            Console.WriteLine("Journal Entry DocEntry  " & sJV, sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Journal Entry DocEntry  " & sJV, sFuncName)
            JournalEntry_Posting = RTN_SUCCESS

        Catch ex As Exception
            p_sJournalEntryError = "Error Description :- " & ex.Message
            Call WriteToLogFile(ex.Message, sFuncName)
            Console.WriteLine("Completed with ERROR ", sFuncName)
            If p_iDebugMode = DEBUG_ON Then Call WriteToLogFile_Debug("Completed with ERROR " & ex.Message, sFuncName)
            JournalEntry_Posting = RTN_ERROR
            Exit Function
        End Try

    End Function

End Module
